<?php

/**
 * @author Tuan Anh
 * @copyright 2012
 */

class recordController extends baseController{
    public function index(){
//        //echo $this->registry->template->getTemplate("record", array());
//        $this->registry->db->getManager("recordManager")->getRecordList();
//        $recordsModule="";
//        while($record = mysql_fetch_assoc($this->registry->db->result['content'])){
//            $tags = array(
//                "rid"               => $record['rid'],
//                "title"             => $record['title'],
//                "price"             => $record['price'],
//                "area"              => $record['area'],
//                "address"           => $record['rid'],
//                "district"             => $record['district'],
//                "province"             => $record['province'],
//                "post_date"         => $record['post_date'],
//            );
//
//            $recordModule = $this->registry->template->getTemplate("article", $tags);
//
//            $recordsModule .= $recordModule;
//        }
//        $tags = array(
//            "left_content" => $recordsModule,
//            "right_content"=>""//$this->registry->template->getTemplate("right_content", array())
//        );
//        $_SESSION['tags_index']['contentModule'] = $this->registry->template->getTemplate("record_list", $tags);//$recordsModule;
//        echo $this->registry->template->getTemplate("index", $_SESSION['tags_index']);
    }
    
    public function dangtin(){
        if(isset($_POST['submit'])){      //if site has post
            if(isset($_SESSION['uid']))
                $_POST['uid'] = $_SESSION['uid'];
            if(isset($_FILES['file'])){
                if ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpg")|| ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 1000000))
                {
                    if ($_FILES["file"]["error"] > 0)
                    {
                        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
                    }
                    else
                    {
                        echo "Upload: " . $_FILES["file"]["name"] . "<br />";
                        echo "Type: " . $_FILES["file"]["type"] . "<br />";
                        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
                        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

                        if (file_exists("../views/image/upload/" . $_FILES["file"]["name"]))
                        {
                            echo $_FILES["file"]["name"] . " already exists. ";
                        }
                        else
                        {
                            move_uploaded_file($_FILES["file"]["tmp_name"], dirname(dirname(__FILE__))."\\views\\images\\upload\\" . $_FILES["file"]["name"]);
                        }
                    }
                }
                else
                {
//                    echo "Invalid file";
                    $_POST['image'] = "";
                }

                $_POST['image'] = $_FILES["file"]["name"];
            }  else{
                $_POST['image'] = "";
            }



            $rid = $this->registry->db->getManager("recordManager")->insert($_POST);
            if($rid == 0){
                $this->master($this->registry->template->masterbody("Đăng tin thất bại! Vui lòng nhập đầy đủ thông tin"));
            }else{
                header("Location:".__SERVER."/record/map?rid=".$rid."&address=".$_POST['address']." ,".$_POST['district'].", ".$_POST['province']);
                $this->master($this->registry->template->masterbody("Thành công!"));
            }

        }else{                  //if site has no post=>newrecordView
            $this->master($this->registry->template->dangtin());
        }
    }

    public function map(){
        if(!isset($_SESSION['uid']))
            $uid = 0;
        else
            $uid =  $_SESSION['uid'];
        $rid = $_GET['rid'];
        $address = $_GET['address'];
        $this->master($this->registry->template->dangtin_map($uid, $rid, $address));
    }
    
    public function edit(){
        $rid = mysql_real_escape_string($_GET['rid']);
        if(isset($_POST['submit'])){
            if($this->registry->db->getManager("recordManager")->edit($rid, $_POST) == -1){
                $this->master($this->registry->template->masterbody("Cập nhật thất bại"));
            }else{
                $this->master($this->registry->template->masterbody("Cập nhật thành công"));
            }
        }else{
            $this->master($this->registry->template->edit($rid)) ;
        } 
    }
    
    public function detail(){
        $rid = $_GET['rid'];
        
        echo $this->master($this->registry->template->detail($rid));

        //$result = $this->registry->db->getManager("recordManager")->detail($rid);
//        $tags = array(
//            "title" => $result['title'],
//            "contact_name" => $result['contact_name'],
//            "contact_mobile" => $result['contact_mobile'],
//            "address" => $result['address'],
//            "district" => $result['did'],
//            "province" => $result['pid'],
//            "area" => $result['area'],
//            "description" => $result['description'],
//            "lng"           => $result['lng'],
//            "lat"           => $result['lat']
//        );
//        
//        //$_SESSION['tags_index']['contentModule'] = $this->registry->template->getTemplate("detail", $tags);
//        echo $this->registry->template->getTemplate("content", $tags);
    }
    public function delete(){
        $this->registry->db->getManager('recordManager')->delete($_GET['rid']);
        header("Location:".__SERVER."/member/tindadang");
    }
    public function updatecoor(){
        $rid = $_GET['rid'];
        $lat = $_GET['lat'];
        $lng = $_GET['lng'];
        $this->registry->db->getManager('recordManager')->updatecoor($rid, $lat, $lng);
    }


    
}

?>